package com.meteor.downloader.videodownloader

import android.content.Intent
import android.os.Handler
import android.os.Looper
import android.os.Message
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform
import com.gyf.immersionbar.BarHide
import com.meteor.downloader.videodownloader.activity.HIDE_ALL
import com.meteor.downloader.videodownloader.activity.MainMdmActivity
import com.meteor.downloader.videodownloader.activity.MdmBindingActivity
import com.meteor.downloader.videodownloader.activity.MdmLanguageInitActivity
import com.meteor.downloader.videodownloader.admdm.MdmDealAdUtils
import com.meteor.downloader.videodownloader.admdm.ShowPageAdListener
import com.meteor.downloader.videodownloader.databinding.ActivityStartMdmBinding
import com.meteor.downloader.videodownloader.utils.ApplicationMdmUtils
import com.meteor.downloader.videodownloader.utils.DataManagerMdmUtils
import com.meteor.downloader.videodownloader.utils.LogInitMdmUtils
import com.meteor.downloader.videodownloader.utils.PermissionMdmUtils
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_CUR_NOTIFY_AD_SHOW
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_ENTER_MAIN_RESULT
import com.meteor.downloader.videodownloader.utils.value.ConstantMdm.MDM_NOTIFY_PERMISSION_TIME
import com.meteor.downloader.videodownloader.utils.value.ParamsMdmUtils

class StartMdmActivity : MdmBindingActivity<ActivityStartMdmBinding>(mBarShow = HIDE_ALL) {
    private var mAgainStart = "no"
    private var mBackGroundShow = false
    private var mBookShowing = false
    private var mMdmNotifyType = ""
    private var mClueLink = ""
    private var mUmpResult = false
    private var mMdmNotifyGrandTotal = 0

    init {
        mHandlerMdm = object : Handler(Looper.getMainLooper()) {
            override fun handleMessage(msg: Message) {
                when (msg.what) {
                    16381 -> {
                        startMdmActivity()
                    }

                    16382 -> {
                        if (!ParamsMdmUtils.mMdmInitUmp && mMdmBinding.progressStart.progress == 10) {
                            sendEmptyMessageDelayed(5382, 150)
                            return
                        }
                        if (mMdmBinding.progressStart.progress >= 100) {
                            mMdmBinding.progressStart.progress = 100
//                            sendEmptyMessage(16381)
                            sendEmptyMessage(16384)
                        } else {
                            mMdmBinding.progressStart.progress += 1
//                            mMdmBinding.progressStart.progress += 10
                            sendEmptyMessageDelayed(16382, 156)
                        }
                    }

                    16383 -> {
                        getUmpValue {
                            if (mUmpResult) return@getUmpValue
                            mUmpResult = true
                            ParamsMdmUtils.mMdmInitUmp = true
                            MdmDealAdUtils.loadBookOpenMdm()
                        }
                    }

                    16384 -> {
                        if (mBookShowing) return
                        mBookShowing = true
                        if (!MdmDealApplication.mAppMdmOpen) {
                            mBackGroundShow = true
                            return
                        }
                        mMdmBinding.progressStart.progress = 100
                        MdmDealAdUtils.showTionOpenValue(
                            this@StartMdmActivity,
                            object : ShowPageAdListener() {
                                override fun successBack() {
                                    MdmDealAdUtils.loadBookNativeMdm()
                                    if (mMdmNotifyType == "true") {
                                        DataManagerMdmUtils.setDataKeyMdm(MDM_CUR_NOTIFY_AD_SHOW, 1)
                                    }
                                }

                                override fun dismissAFailed() {
                                    startMdmActivity()
                                }
                            })
                    }
                }
            }
        }
    }

    override fun beginViewAndDoMdm() {
        mMdmBinding.progressStart.max = 100
        intent?.let {
            mMdmNotifyType = it.getStringExtra("mdmNotifyType") ?: ""
            mMdmNotifyGrandTotal = it.getIntExtra("mdmNotifyGrandTotal", 0)
            if (mMdmNotifyType == "true") {
                LogInitMdmUtils.clueMdmValue(this, "click")
            }
            mClueLink = it.getStringExtra("clueLink") ?: ""
            if (mClueLink.isNotEmpty()) {
                LogInitMdmUtils.clueCompleteValue(this, "click")
            }
        }
        if (System.currentTimeMillis() - DataManagerMdmUtils.getDataKeyMdm(
                MDM_NOTIFY_PERMISSION_TIME, 0L
            ) > 8 * 3600000
        ) {
            PermissionMdmUtils.getNotifyPermissionApply(this) {
                DataManagerMdmUtils.setDataKeyMdm(
                    MDM_NOTIFY_PERMISSION_TIME, System.currentTimeMillis()
                )
                mHandlerMdm?.sendEmptyMessageDelayed(16382, 220)
                mHandlerMdm?.sendEmptyMessageDelayed(16383, 220)
            }
        } else {
            mHandlerMdm?.sendEmptyMessageDelayed(16382, 220)
            mHandlerMdm?.sendEmptyMessageDelayed(16383, 220)
        }
        ApplicationMdmUtils.getMeteorVideoSome()
    }

    override fun loadBookOpenMdm(result: Boolean) {
        mHandlerMdm?.sendEmptyMessage(16384)
    }

    private fun getUmpValue(mdmBack: () -> Unit) {
        if (ParamsMdmUtils.mMdmInitUmp) {
            mdmBack()
        } else {
            val params = ConsentRequestParameters.Builder().build()
            UserMessagingPlatform.getConsentInformation(this).let {
                it.requestConsentInfoUpdate(this, params, {
                    UserMessagingPlatform.loadAndShowConsentFormIfRequired(this) {
                        mdmBack()
                    }
                }, {
                    mdmBack()
                })
                if (it.canRequestAds()) {
                    mdmBack()
                }
            }
        }
    }

    private fun startMdmActivity() {
        if (isFinishing || isDestroyed) return
        if (!MdmDealApplication.mAppMdmOpen) {
            mAgainStart = "yes"
            return
        }
        if (!DataManagerMdmUtils.getDataKeyMdm(MDM_ENTER_MAIN_RESULT, false)) {
            startActivity(Intent(this, MdmLanguageInitActivity::class.java))
        } else {
            startActivity(Intent(this, MainMdmActivity::class.java).apply {
                putExtra("mdmNotifyType", mMdmNotifyType)
                putExtra("mdmNotifyGrandTotal", mMdmNotifyGrandTotal)
            })
        }
        finish()
    }

    override fun onResume() {
        super.onResume()
        if (mAgainStart == "yes") {
            mAgainStart = "no"
            startMdmActivity()
        } else {
            if (mBackGroundShow) {
                mBackGroundShow = false
                mBookShowing = false
                mHandlerMdm?.sendEmptyMessage(16384)
            }
        }
    }

    override fun getLayoutValue(): ActivityStartMdmBinding {
        return ActivityStartMdmBinding.inflate(layoutInflater)
    }
}